package com.example.controller;

import com.example.pojo.User;
import com.example.service.UserService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/allUser")
    public String allUser(Model model) {
        List<User> allUser = userService.getAllUser();
        model.addAttribute("allUser", allUser);
        return "allUser";
    }

    @GetMapping("/toAddUser")
    public String toAddUser(Model model) {
        return "addUser";
    }

    @PostMapping("/addUser")
    public String addUser(Model model, User newUser) {
        System.out.println(userService.addUser(newUser));
        return allUser(model);
    }

    @GetMapping("/deleteUser/{id}")
    public String deleteUser(Model model, @PathVariable("id") int id) {
        System.out.println(userService.deleteUserById(id));
        return allUser(model);
    }

    @GetMapping("/toUpdateUserPage")
    public String toUpdateUser(Model model, User needUpdateUser) {
        model.addAttribute("needUpdateUser", needUpdateUser);
        return "updateUser";
    }
    @PostMapping("/updateUser")
    public String updateUser(Model model, User updatedUser) {
        System.out.println(userService.updateUser(updatedUser));
        return allUser(model);
    }

}
